package com.bsb.biz.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.bsb.base.common.config.Accounting;
import com.bsb.base.common.util.StringUtils;
import com.bsb.base.model.BsbAccounting;
import com.bsb.base.model.BsbAccountingExample;
import com.bsb.base.model.BsbAccountingExample.Criteria;
import com.bsb.biz.service.BsbAccountingService;
import com.bsb.biz.shiro.UserUtils;

@Controller
@RequestMapping("/bsbAccounting")
public class BsbAccountingController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/bsbAccounting/list.do";
	
	@Resource
	private BsbAccountingService bsbAccountingService;
	
	
	public void setCommon(Model model){
		List<Accounting> list = Accounting.getList();
		model.addAttribute("types", list);
	}
	
	@RequiresPermissions("bsbAccounting:view")
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String list(
			Model model,@RequestParam Map<String, Object> params,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		String name = (String) params.get("name");
		String code = (String) params.get("code");
		String type = (String) params.get("type");
		BsbAccountingExample bsbAccountingExample = new BsbAccountingExample();
		Criteria criteria = bsbAccountingExample.createCriteria();
		if(StringUtils.isNotEmpty(name)){
			criteria.andNameLike("%"+name+"%");
		}
		if(StringUtils.isNotEmpty(code)){
			criteria.andCodeLike("%"+code+"%");
		}
		if(StringUtils.isNotEmpty(type)){
			criteria.andTypeEqualTo(type);
		}
		int totalCount = bsbAccountingService.countBsbAccounting(bsbAccountingExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		bsbAccountingExample.setPageIndex((pageNum - 1) * pageSize);
		bsbAccountingExample.setPageSize(pageSize);
		bsbAccountingExample.setOrderByClause("insertTime DESC");
		List<BsbAccounting> bsbAccountings = bsbAccountingService.getPageBsbAccounting(bsbAccountingExample);
		for (BsbAccounting bsbAccounting : bsbAccountings) {
			bsbAccounting.setType(Accounting.getName(bsbAccounting.getType()));
		}
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		setCommon(model);
		model.addAttribute("bsbAccountings", bsbAccountings);
		model.addAttribute("name", name);
		model.addAttribute("code", code);
		model.addAttribute("type", type);
		return "/BsbAccounting/list";
	}
	
	@RequiresPermissions("bsbAccounting:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String toAdd(Model model) {
		setCommon(model);
		return "/BsbAccounting/add";
	}

	@RequiresPermissions("bsbAccounting:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String add(Model model, @ModelAttribute("bsbAccounting") BsbAccounting bsbAccounting) {
		bsbAccounting.setInsertId(UserUtils.currentUser().getId().toString());
		bsbAccountingService.addBsbAccounting(bsbAccounting);
		return redirect;
	}
	
	@RequiresPermissions("bsbAccounting:delete")
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String del(Model model,RedirectAttributes redirectAttributes,
			HttpServletRequest request,
			@PathVariable("id") Integer id) {
		String queryString = request.getQueryString();
		bsbAccountingService.delBsbAccountingById(id);
		return redirect+ "?" +queryString;
	}
	
	@RequiresPermissions("bsbAccounting:update")
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String toEdit(Model model,RedirectAttributes redirectAttributes,
			HttpServletRequest request,
			@PathVariable("id") Integer id) {
		setCommon(model);
		BsbAccounting bsbAccounting = bsbAccountingService.getBsbAccountingById(id);
		String queryString = request.getQueryString();
		model.addAttribute("queryString", queryString);
		model.addAttribute("bsbAccounting", bsbAccounting);
		return "/BsbAccounting/edit";
	}

	@RequiresPermissions("bsbAccounting:update")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String edit(Model model,@RequestParam String queryString,
			@ModelAttribute("bsbAccounting") BsbAccounting bsbAccounting) {
		bsbAccounting.setUpdateId(UserUtils.currentUser().getId().toString());
		bsbAccountingService.updateBsbAccounting(bsbAccounting);
		return redirect+ "?" +queryString;
	}
	
	@RequiresPermissions("bsbAccounting:view")
	@RequestMapping(value = "/{id}/view", method = RequestMethod.GET)
	public String view(Model model,
			@PathVariable("id") Integer id) {
		BsbAccounting bsbAccounting = bsbAccountingService.getBsbAccountingById(id);
		if(null!=bsbAccounting){
			bsbAccounting.setType(Accounting.getName(bsbAccounting.getType()));
		}
		model.addAttribute("bsbAccounting", bsbAccounting);
		return "/BsbAccounting/view";
	}
}
